package biz;

import base.Result;
import com.google.gson.Gson;
import comm.Constrant;
import dao.HomePageDao;
import org.apache.ibatis.session.SqlSession;
import pojo.*;
import util.SqlSessionFactoryUtil;

import java.util.List;

import static comm.Constrant.*;

/**
 * 首页control类
 */
public class HomePageService {
    static SqlSession session;
    static HomePageDao dao;
    static final HomePageService service = new HomePageService();

    static {
        session = SqlSessionFactoryUtil.getSqlSession();
        dao = session.getMapper(HomePageDao.class);
    }

    private HomePageService() {
    }

    public static HomePageService start() {
        return service;
    }

    /**
     * 搜索
     */
    public String search(String content) {
        System.out.println("++++" + content);
        Search search = dao.getSearch(content);
        if (search != null) {//modify
            search.setCount(search.getCount() + 1);
            dao.updateHotSearch(search);
        } else {//create
            dao.insertSearch(new Search(content));
        }
        session.commit();
        return null;
    }

    /**
     * 获取热搜词
     */
    public Result<List<Search>> getHotSearchList() {
        Result<List<Search>> result = new Result<>();
        try {
            List<Search> data = dao.selectSearch();
            session.commit();
            if (data != null) {
                result.setCode(CODE_SUCCESS);
                result.setData(data);
            } else {
                result.setCode(CODE_UNKNOW);
                result.setMessage(errorMessage(CODE_UNKNOW));
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            result.setCode(CODE_UNKNOW);
            result.setMessage(ex.getMessage());
        }
        return result;
    }

    /**
     * 获取首页领域分组以及领域对应的行业列表
     */
    public Result<List<Domain>> getDomains() {
        Result<List<Domain>> result = new Result<>();
        try {
            List<Domain> data = dao.connSelectDomain();
            session.commit();
            if (data != null) {
                result.setCode(CODE_SUCCESS);
                result.setData(data);
            } else {
                result.setCode(CODE_UNKNOW);
                result.setMessage(errorMessage(CODE_UNKNOW));
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            result.setCode(CODE_UNKNOW);
            result.setMessage(ex.getMessage());
        }
        return result;
    }

    /**
     * 获取工作列表
     *
     * @param order 1.热度排序，2.时间排序
     */
    public Result<List<Job>> getJobList(int order, Object page) {
        Result<List<Job>> result = new Result<>();
        try {
            List<Job> data;
            if (order == 1) {
                data = dao.getJobListOrderByCount();
            } else if (order == 2) {
                data = dao.getJobListOrderByTime();
            } else {
                data = dao.getJobList();
            }
            if (data != null) {
                List<Job> jobs = null;
                if (page != null) {
                    int pageIndex;
                    if ((int) page == -1) {
                        pageIndex = data.size();
                    } else {
                        pageIndex = (int) page;
                    }
                    int start = (pageIndex - 1) * PAGE_SIZE;
                    int end = pageIndex * PAGE_SIZE;
                    if (data.size() < end) {
                        end = data.size();
                        start = end - PAGE_SIZE;
                    }
                    if (PAGE_SIZE > data.size()) {
                        jobs = data;
                    } else {
                        jobs = data.subList(start, end);
                    }
                }
                result.setCode(CODE_SUCCESS);
                result.setData(jobs);
                if (data.size() % PAGE_SIZE > 0) {
                    result.setTotal((data.size() / PAGE_SIZE) + 1);
                } else {
                    result.setTotal(data.size() / PAGE_SIZE);
                }
            } else {
                result.setCode(CODE_UNKNOW);
                result.setMessage(errorMessage(CODE_UNKNOW));
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            result.setCode(CODE_UNKNOW);
            result.setMessage(ex.getMessage());
        }
        return result;
    }

    /**
     * 获取工作列表(筛选条件)
     */
    public Result<List<Job>> getExceptJobList(String cond, Object page) {
        Result<List<Job>> result = new Result<>();
        try {
            List<Job> data;
            data = dao.getExceptJobList(cond);
            if (data != null) {
                List<Job> jobs = null;
                if (page != null) {
                    int pageIndex;
                    if ((int) page == -1) {
                        pageIndex = data.size();
                    } else {
                        pageIndex = (int) page;
                    }
                    int start = (pageIndex - 1) * PAGE_SIZE;
                    int end = pageIndex * PAGE_SIZE;
                    if (data.size() < end) {
                        end = data.size();
                        start = end - PAGE_SIZE;
                    }
                    if (PAGE_SIZE > data.size()) {
                        jobs = data;
                    } else {
                        jobs = data.subList(start, end);
                    }
                }
                result.setCode(CODE_SUCCESS);
                result.setData(jobs);
                if (data.size() % PAGE_SIZE > 0) {
                    result.setTotal((data.size() / PAGE_SIZE) + 1);
                } else {
                    result.setTotal(data.size() / PAGE_SIZE);
                }
            } else {
                result.setCode(CODE_UNKNOW);
                result.setMessage(errorMessage(CODE_UNKNOW));
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            result.setCode(CODE_UNKNOW);
            result.setMessage(ex.getMessage());
        }
        return result;
    }

    /**
     * 获取公司列表
     *
     * @param order 1.热度排序，2.时间排序
     */
    public Result<List<Company>> getCompanyList(int order, Object page) {
        Result<List<Company>> result = new Result<>();
        try {
            List<Company> data;
            if (order == 1) {
                data = dao.getCompanyListByTime();
            } else if (order == 2) {
                data = dao.getCompanyListByCount();
            } else {
                data = dao.getCompanyList();
            }
            if (data != null) {
                List<Company> companies = null;
                if (page != null) {
                    int pageIndex;
                    if ((int) page == -1) {
                        pageIndex = data.size();
                    } else {
                        pageIndex = (int) page;
                    }
                    int start = (pageIndex - 1) * PAGE_SIZE;
                    int end = pageIndex * PAGE_SIZE;
                    if (data.size() < end) {
                        end = data.size();
                        start = end - PAGE_SIZE;
                    }
                    companies = data.subList(start, end);
                }
                result.setCode(CODE_SUCCESS);
                result.setData(companies);
                if (data.size() % PAGE_SIZE > 0) {
                    result.setTotal((data.size() / PAGE_SIZE) + 1);
                } else {
                    result.setTotal(data.size() / PAGE_SIZE);
                }
            } else {
                result.setCode(CODE_UNKNOW);
                result.setMessage(errorMessage(CODE_UNKNOW));
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            result.setCode(CODE_UNKNOW);
            result.setMessage(ex.getMessage());
        }
        return result;
    }

    /**
     * 获取标签列表
     */
    public Result<List<JobTips>> getJobTipsList(String industry) {
        Result<List<JobTips>> result = new Result<>();
        try {
            List<JobTips> data = dao.getJobTipsList(industry);
            if (data != null) {
                result.setCode(CODE_SUCCESS);
                result.setData(data);
            } else {
                result.setCode(CODE_UNKNOW);
                result.setMessage(errorMessage(CODE_UNKNOW));
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            result.setCode(CODE_UNKNOW);
            result.setMessage(ex.getMessage());
        }
        return result;
    }

    /**
     * 获取工作种类
     */
    public Result<List<Industry>> getIndustries() {
        Result<List<Industry>> result = new Result<>();
        try {
            List<Industry> data = dao.getIndustries();
            if (data != null) {
                result.setCode(CODE_SUCCESS);
                result.setData(data);
            } else {
                result.setCode(CODE_UNKNOW);
                result.setMessage(errorMessage(CODE_UNKNOW));
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            result.setCode(CODE_UNKNOW);
            result.setMessage(ex.getMessage());
        }
        return result;
    }

    /**
     * 新增工作
     */
    public Result<String> createJob(Job job) {
        Result<String> result = new Result<>();
        try {
            int callback = dao.createJob(job);
            session.commit();
            if (callback > 0) {
                result.setCode(CODE_SUCCESS);
                result.setData("发布成功！");
            } else {
                result.setCode(CODE_UNKNOW);
                result.setMessage(errorMessage(CODE_UNKNOW));
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            result.setCode(CODE_UNKNOW);
            result.setMessage(ex.getMessage());
        }
        return result;
    }

    /**
     * 新增公司
     */
    public Result<String> createCompany(Company company) {
        Result<String> result = new Result<>();
        try {
            int callback = dao.createCompany(company);
            session.commit();
            if (callback > 0) {
                result.setCode(CODE_SUCCESS);
                result.setData("发布成功！");
            } else {
                result.setCode(CODE_UNKNOW);
                result.setMessage(errorMessage(CODE_UNKNOW));
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            result.setCode(CODE_UNKNOW);
            result.setMessage(ex.getMessage());
        }
        return result;
    }

    public void closeSession() {
        session.close();
    }

}
